import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model - 租户维护
 * @date: 2018-8-9

 */
import { getResponse, createPagination } from 'utils/utils';
import { queryTenant as _queryTenant, updateTenant as _updateTenant, addTenant as _addTenant, queryTenantDetail as _queryTenantDetail } from "../services/tenantsService";
export default {
  namespace: 'tenants',
  state: {
    tenantData: {},
    // 租户数据
    tenantDataDetail: {},
    // 租户明细数据
    pagination: {} // 分页参数

  },
  effects: {
    // 查询数据
    queryTenant: /*#__PURE__*/_regeneratorRuntime.mark(function queryTenant(_ref, _ref2) {
      var payload, call, put, response, tenantData;
      return _regeneratorRuntime.wrap(function queryTenant$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(_queryTenant, payload);

            case 4:
              response = _context.sent;
              tenantData = getResponse(response);

              if (!tenantData) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  tenantData: tenantData,
                  pagination: createPagination(tenantData)
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, queryTenant);
    }),
    // 查询数据
    queryTenantDetail: /*#__PURE__*/_regeneratorRuntime.mark(function queryTenantDetail(_ref3, _ref4) {
      var payload, call, put, response, tenantDataDetail;
      return _regeneratorRuntime.wrap(function queryTenantDetail$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context2.next = 4;
              return call(_queryTenantDetail, payload);

            case 4:
              response = _context2.sent;
              tenantDataDetail = getResponse(response);

              if (!tenantDataDetail) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  tenantDataDetail: tenantDataDetail
                }
              });

            case 9:
              return _context2.abrupt("return", tenantDataDetail);

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, queryTenantDetail);
    }),
    // 编辑数据
    updateTenant: /*#__PURE__*/_regeneratorRuntime.mark(function updateTenant(_ref5, _ref6) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function updateTenant$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context3.next = 4;
              return call(_updateTenant, payload);

            case 4:
              response = _context3.sent;
              return _context3.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, updateTenant);
    }),
    // 新增数据
    addTenant: /*#__PURE__*/_regeneratorRuntime.mark(function addTenant(_ref7, _ref8) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function addTenant$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_addTenant, payload);

            case 4:
              response = _context4.sent;
              return _context4.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, addTenant);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};